package com.xmy.study.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.study.entity.ChaPlan;
import com.xmy.study.entity.EduTask;
import com.xmy.study.entity.views.PlanShutNum;
import com.xmy.study.service.IChaPlanService;
import com.xmy.study.service.IEduTaskService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/api/outside/chaPlan")
public class ChaPlanController {

    @Autowired
    @Lazy
    IChaPlanService iChaPlanService;

    @Autowired
    @Lazy
    IEduTaskService eduTaskService;

    /**
     * 获取计划
     *
     * @param chaPlan
     * @return
     */
    @ApiOperation("获取计划")
    @GetMapping("getChaPlan")
    public ChaPlan getChaPlan(@RequestParam ChaPlan chaPlan) {
        QueryWrapper<ChaPlan> chaPlanQueryWrapper = new QueryWrapper<>();
        chaPlanQueryWrapper.eq("student_grade_id", chaPlan.getStudentGradeId());
        chaPlanQueryWrapper.eq("student_id", chaPlan.getStudentId());
        chaPlanQueryWrapper.eq("school_id", chaPlan.getSchoolId());
        chaPlanQueryWrapper.eq("year_part", chaPlan.getYearPart());
        chaPlanQueryWrapper.eq("year_class_id", chaPlan.getYearClassId());
        chaPlanQueryWrapper.eq("quarter_num", chaPlan.getQuarterNum());
        chaPlanQueryWrapper.eq("lesson_num", chaPlan.getLessonNum());
        chaPlanQueryWrapper.eq("school_date", chaPlan.getSchoolDate());
        chaPlanQueryWrapper.last("LIMIT 1");
        return iChaPlanService.getOne(chaPlanQueryWrapper);
    }

    /**
     * 未完成计划
     * @param reMap
     * @return
     */
    @ApiOperation("未完成计划")
    @PostMapping("getChaPlanList")
    public List<ChaPlan> getChaPlanList(@RequestBody Map<String, Object> reMap) {
        String schoolDate = null;
        if (reMap.get("schoolDate") != null && !reMap.get("schoolDate").toString().isEmpty()){
            schoolDate = reMap.get("schoolDate").toString();
        }
        String schoolId = null;
        if (reMap.get("schoolId") != null && !reMap.get("schoolId").toString().isEmpty()){
            schoolId = reMap.get("schoolId").toString();
        }
        QueryWrapper<ChaPlan> chaPlanQueryWrapper = new QueryWrapper<>();
        chaPlanQueryWrapper.le("school_date", schoolDate);
        if (schoolId != null && !schoolId.isEmpty()) {
            chaPlanQueryWrapper.eq("school_id", schoolId);
        }
        String yearClassIdString = null;
        //年级
        if (reMap.get("yearClassId") != null && !reMap.get("yearClassId").toString().isEmpty()) {
            yearClassIdString = reMap.get("yearClassId").toString();
        }
        //学段
        if (reMap.get("gradeSection") != null && !reMap.get("gradeSection").toString().isEmpty()) {
            yearClassIdString = reMap.get("gradeSection").toString();
        }
        if (yearClassIdString != null) {
            chaPlanQueryWrapper.eq("year_class_id", yearClassIdString);
        }
        if (reMap.get("teacherId") != null && !reMap.get("teacherId").toString().isEmpty()) {
            chaPlanQueryWrapper.eq("teacher_id", reMap.get("teacherId"));
        }
        if (reMap.get("gradeId") != null && !reMap.get("gradeId").toString().isEmpty()) {
            chaPlanQueryWrapper.eq("grade_id", reMap.get("gradeId"));
        }
//        if (reMap.get("courseTypeId") != null && !reMap.get("courseTypeId").toString().isEmpty()) {
//            chaPlanQueryWrapper.eq("course_type_id", reMap.get("courseTypeId"));
//        }
        chaPlanQueryWrapper.gt("lowest_shutnum - right_num - amend_num",0);
        return iChaPlanService.list(chaPlanQueryWrapper);
    }

    /**
     * 已完成计划
     * @param reMap
     * @return
     */
    @ApiOperation("已完成计划")
    @PostMapping("getChaPlanListYes")
    public List<ChaPlan> getChaPlanListYes(@RequestBody Map<String, Object> reMap) {
        String schoolDate = null;
        if (reMap.get("schoolDate") != null && !reMap.get("schoolDate").toString().isEmpty()){
            schoolDate = reMap.get("schoolDate").toString();
        }
        String schoolId = null;
        if (reMap.get("schoolId") != null && !reMap.get("schoolId").toString().isEmpty()){
            schoolId = reMap.get("schoolId").toString();
        }
        QueryWrapper<ChaPlan> chaPlanQueryWrapper = new QueryWrapper<>();
        chaPlanQueryWrapper.le("school_date", schoolDate);
        if (schoolId != null && !schoolId.isEmpty()) {
            chaPlanQueryWrapper.eq("school_id", schoolId);
        }
        String yearClassIdString = null;
        //年级
        if (reMap.get("yearClassId") != null && !reMap.get("yearClassId").toString().isEmpty()) {
            yearClassIdString = reMap.get("yearClassId").toString();
        }
        //学段
        if (reMap.get("gradeSection") != null && !reMap.get("gradeSection").toString().isEmpty()) {
            yearClassIdString = reMap.get("gradeSection").toString();
        }
        if (yearClassIdString != null) {
            chaPlanQueryWrapper.eq("year_class_id", yearClassIdString);
        }
        if (reMap.get("teacherId") != null && !reMap.get("teacherId").toString().isEmpty()) {
            chaPlanQueryWrapper.eq("teacher_id", reMap.get("teacherId"));
        }
        if (reMap.get("gradeId") != null && !reMap.get("gradeId").toString().isEmpty()) {
            chaPlanQueryWrapper.eq("grade_id", reMap.get("gradeId"));
        }
//        if (reMap.get("courseTypeId") != null && !reMap.get("courseTypeId").toString().isEmpty()) {
//            chaPlanQueryWrapper.eq("course_type_id", reMap.get("courseTypeId"));
//        }
        chaPlanQueryWrapper.eq("lowest_shutnum - right_num - amend_num",0);
        return iChaPlanService.list(chaPlanQueryWrapper);
    }


    /**
     * 保存计划
     *
     * @param chaPlan
     * @return
     */
    @ApiOperation("保存计划")
    @PostMapping("saveChaPlan")
    public boolean saveChaPlan(@RequestBody ChaPlan chaPlan) {
        QueryWrapper<ChaPlan> chaPlanQueryWrapper = new QueryWrapper<>();
        chaPlanQueryWrapper.eq("student_grade_id", chaPlan.getStudentGradeId());
        chaPlanQueryWrapper.eq("student_id", chaPlan.getStudentId());
        chaPlanQueryWrapper.eq("school_id", chaPlan.getSchoolId());
        chaPlanQueryWrapper.eq("year_part", chaPlan.getYearPart());
        chaPlanQueryWrapper.eq("year_class_id", chaPlan.getYearClassId());
        chaPlanQueryWrapper.eq("quarter_num", chaPlan.getQuarterNum());
        chaPlanQueryWrapper.eq("school_date", chaPlan.getSchoolDate());
        chaPlanQueryWrapper.eq("teacher_id", chaPlan.getTeacherId());
        chaPlanQueryWrapper.eq("grade_id", chaPlan.getGradeId());
        chaPlanQueryWrapper.last("LIMIT 1");
        ChaPlan chaPlanServiceOne = iChaPlanService.getOne(chaPlanQueryWrapper);
        QueryWrapper<EduTask> eduTaskQueryWrapper = new QueryWrapper<>();
        eduTaskQueryWrapper.eq("year_part", chaPlan.getYearPart());
        eduTaskQueryWrapper.eq("year_class_id", chaPlan.getYearClassId());
        eduTaskQueryWrapper.eq("quarter_num", chaPlan.getQuarterNum());
        Calendar calendar = Calendar.getInstance();
        String[] date = chaPlan.getSchoolDate().toString().split("-");
        calendar.set(Integer.parseInt(date[0]), Integer.parseInt(date[1]) - 1, Integer.parseInt(date[2]));
        int monthPart = calendar.get(calendar.MONTH);//当前月份
        int yearPart = calendar.get(calendar.YEAR);//当前年份
        int todayPart = calendar.get(calendar.DAY_OF_MONTH);//当前天数
        int week = calendar.get(calendar.DAY_OF_WEEK);
        if (week == 1) {
            calendar.set(yearPart, monthPart, (todayPart - 6));
        } else {
            calendar.set(yearPart, monthPart, (todayPart - (week - 2)));
        }
        int todayPartStart = calendar.get(calendar.DAY_OF_MONTH);//开始天数
        int monthPartStart = calendar.get(calendar.MONTH) + 1;//开始月份
        int yearPartStart = calendar.get(calendar.YEAR);//开始年份
        String oenDate = yearPartStart + "-" + monthPartStart + "-" + todayPartStart + " 00:00:00";
        if (week == 1) {
            calendar.set(yearPart, monthPart, todayPart);
        } else {
            calendar.set(yearPart, monthPart, (todayPart + (7 - week) + 1));
        }
        int todayPartEnd = calendar.get(calendar.DAY_OF_MONTH);//结束天数
        int monthPartEnd = calendar.get(calendar.MONTH) + 1;//结束月份
        int yearPartEnd = calendar.get(calendar.YEAR);//结束年份
        eduTaskQueryWrapper.between("task_date", oenDate, yearPartEnd + "-" + monthPartEnd + "-" + todayPartEnd + " 23:59:59");
        eduTaskQueryWrapper.last("LIMIT 1");
        EduTask eduTaskServiceOne = eduTaskService.getOne(eduTaskQueryWrapper);
        if (eduTaskServiceOne != null) {
            chaPlan.setLessonNum(eduTaskServiceOne.getTaskCourseNum());
            chaPlan.setEduTaskId(eduTaskServiceOne.getId());
            chaPlan.setLowestShutnum(eduTaskServiceOne.getLowestShutnum());
            System.out.println("没有数据？");
        }
        if (chaPlanServiceOne != null) {
            chaPlan.setId(chaPlanServiceOne.getId());
            return iChaPlanService.updateById(chaPlan);
        }
        return iChaPlanService.save(chaPlan);
    }

    @ApiOperation("通关率-查询期间为当前周一至周日")
    @PostMapping("getClearanceRateChaPlan")
    public ChaPlan getClearanceRateChaPlan(@RequestBody PlanShutNum planShutNum) {
        Calendar calendar = Calendar.getInstance();
        if (planShutNum.getTaskDateStart() != null) {
            String  schoolDate = planShutNum.getTaskDateStart();
            String[] date =  schoolDate.split("-");
            calendar.set(Integer.parseInt(date[0]), Integer.parseInt(date[1]) - 1, Integer.parseInt(date[2]));
        }
        int monthPart = calendar.get(calendar.MONTH);//当前月份
        int yearPart = calendar.get(calendar.YEAR);//当前年份
        int todayPart = calendar.get(calendar.DAY_OF_MONTH);//当前天数
        int week = calendar.get(calendar.DAY_OF_WEEK);
        if (week == 1) {
            calendar.set(yearPart, monthPart, (todayPart - 6));
        } else {
            calendar.set(yearPart, monthPart, (todayPart - (week - 2)));
        }
        int todayPartStart = calendar.get(calendar.DAY_OF_MONTH);//开始天数
        int monthPartStart = calendar.get(calendar.MONTH) + 1;//开始月份
        int yearPartStart = calendar.get(calendar.YEAR);//开始年份
        if (week == 1) {
            calendar.set(yearPart, monthPart, todayPart);
        } else {
            calendar.set(yearPart, monthPart, (todayPart + (7 - week) + 1));
        }
        int todayPartEnd = calendar.get(calendar.DAY_OF_MONTH);//结束天数
        int monthPartEnd = calendar.get(calendar.MONTH) + 1;//结束月份
        int yearPartEnd = calendar.get(calendar.YEAR);//结束年份
        QueryWrapper<ChaPlan> chaPlanQueryWrapper = new QueryWrapper<>();
        chaPlanQueryWrapper.in("c.student_id", planShutNum.getStudentIdList());
        chaPlanQueryWrapper.between("c.school_date", yearPartStart+"-"+monthPartStart+"-"+todayPartStart+" 00:00:00",yearPartEnd+"-"+monthPartEnd+"-"+todayPartEnd+" 23:59:59");
        List<ChaPlan> clearanceRateChaPlan = iChaPlanService.getClearanceRateChaPlan(chaPlanQueryWrapper);
        ChaPlan chaPlanReturn = new ChaPlan();
        //初始化
        chaPlanReturn.setLessonNum(0);//总人数
        chaPlanReturn.setAmendNum(0);//修正题数
        chaPlanReturn.setRightNum(0);//正确题数
        chaPlanReturn.setLowestShutnum(0);//应通关
        chaPlanReturn.setYearClassId(0);//通关数
        chaPlanReturn.setQuarterNum(0);//未通关数
        chaPlanReturn.setYearPart(0);//总和是否正常为 0
        if (clearanceRateChaPlan != null && !clearanceRateChaPlan.isEmpty()) {
            chaPlanReturn.setLessonNum(clearanceRateChaPlan.size());
            for (ChaPlan chaPlan : clearanceRateChaPlan) {
//                chaPlanReturn.setAmendNum(chaPlanReturn.getAmendNum() + chaPlan.getAmendNum());
//                chaPlanReturn.setRightNum(chaPlanReturn.getRightNum() + chaPlan.getRightNum());
//                chaPlanReturn.setLowestShutnum(chaPlanReturn.getLowestShutnum() + chaPlan.getLowestShutnum());
                if (chaPlan.getLowestShutnum() - chaPlan.getRightNum() - chaPlan.getRightNum() <= 0) {
                    chaPlanReturn.setYearClassId(chaPlanReturn.getYearClassId() + 1);
                } else {
                    chaPlanReturn.setQuarterNum(chaPlanReturn.getQuarterNum() + 1);
                }
            }
            chaPlanReturn.setYearPart(chaPlanReturn.getLessonNum() - chaPlanReturn.getYearClassId() - chaPlanReturn.getQuarterNum());
            //安条件进行处理
            chaPlanReturn.setAmendNum(chaPlanReturn.getQuarterNum());//末通关:应通关题数·正确题数-修正题数≠0
            chaPlanReturn.setRightNum(chaPlanReturn.getYearClassId());//已通关:应通关题数-正确题数-修正题数=0
            chaPlanReturn.setLowestShutnum(chaPlanReturn.getLessonNum());//总人数 应通关题数
        }
        return chaPlanReturn;
    }
}
